package com.netty.config;

import com.netty.ImWebsocketServer;
import com.netty.constant.ImChannels;
import com.netty.disruptor.DisruptorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

/**
 * @author zl
 * @version 1.0
 * 项目启动成功之后在启动netty
 * @date 2021/9/17 17:56
 */
@Slf4j
@Configuration
public class CIMConfig implements ApplicationListener<ApplicationStartedEvent> {
    @Resource
    private ApplicationContext applicationContext;
    @Autowired
    private ImChannels imChannels;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        try {
            DisruptorUtils disruptorUtils = new DisruptorUtils(10);
            disruptorUtils.createDisruptor();

            ImWebsocketServer imWebsocketServer = new ImWebsocketServer(new CIMWebSocketConfig(imChannels,redisTemplate), 34567);
            imWebsocketServer.startServer();
            ImWebsocketServer imWebsocketServer2 = new ImWebsocketServer(new CIMWebSocketConfig(imChannels,redisTemplate), 45678);
            imWebsocketServer2.startServer();
//            ImSocketServer imSocketServer = new ImSocketServer(new CIMSocketConfig(sessionGroup), 23456);
//            imSocketServer.startServer();
//            ImUdpServer imUdpServer = new ImUdpServer(new CIMUdpConfig(), 45678);
//            imUdpServer.startServer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
